package com.dwin.h5.app.views.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.dwin.h5.app.R;
import com.dwin.h5.app.utils.ToastUtil;


/**
 * 配网输入密码：Dialog
 * 用户可以修改wifi名称
 */
public class ConfigWifiDialogV2 extends Dialog implements View.OnClickListener {

    private Context context;
    private TextView tv_positive;
    private MyDialogListener listener;

    private EditText et_wifi_password, et_wifi_name;
    private String title;
    private String password;

    public interface MyDialogListener {
        public void onClickPositive(String name, String password);
    }

    public ConfigWifiDialogV2 setMyTitle(String title) {
        this.title = title;
        return this;
    }

    public ConfigWifiDialogV2 setMyPwd (String password) {
        this.password = password;
        return this;
    }

    public ConfigWifiDialogV2(Context context) {
        super(context);
        this.context = context;
    }

    public ConfigWifiDialogV2(Context context, int style, MyDialogListener listener) {
        super(context, style);
        this.context = context;
        this.listener = listener;
    }

    //    style引用style样式  dialog_update_layout
    public ConfigWifiDialogV2(Context context, int style) {
        super(context, style);
    }

    public void setDialogListener(MyDialogListener listener) {
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_config_wifi_layoutv2);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);

        findViewById(R.id.iv_close).setOnClickListener(this);
        et_wifi_name = findViewById(R.id.et_wifi_name); // 账号名和密码
        et_wifi_name.setText(title);
        et_wifi_password = findViewById(R.id.et_wifi_password);
        et_wifi_password.requestFocus(); // 焦点默认在密码上
        if (!TextUtils.isEmpty(password)) {
            et_wifi_password.setText(password); // 设置显示密码
            et_wifi_password.setSelection(password.length());
        }
        tv_positive = (TextView) findViewById(R.id.tv_positive);
        tv_positive.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            switch (v.getId()) {
                case R.id.tv_positive:
                    String name = et_wifi_name.getText().toString().trim();
                    String wifiPassword = et_wifi_password.getText().toString().trim();
                    if (!TextUtils.isEmpty(name)) {
                        listener.onClickPositive(name, wifiPassword);
                    } else {
                        ToastUtil.toastShort(context, R.string.input_wifi_name);
                        et_wifi_name.requestFocus();
                    }
                    break;
                case R.id.iv_close:
                    dismiss();
                    break;
            }
        }
    }

}
